package com.cskaoyan.service;

import com.cskaoyan.bean.StorageInfo.StorageVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.util.GetMd5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/05/06 12:27
 *
 * @author wangyonghong
 */
@Service
public class StorageServiceImpl implements StorageService {

    // by liutao
    @Value("${spring.resources.static-locations}")
    String locations;

    @Autowired
    MarketStorageMapper marketStorageMapper;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    static Date birthday = new Date();

    @SneakyThrows
    @Override
    public MarketStorage Create(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        int size = (int) file.getSize();
        String contentType = file.getContentType();

//        file.transferTo(new File("D:\\image",originalFilename));
//        String url="http://localhost:8083/image/" + originalFilename;
//        MarketStorage marketStorage=new MarketStorage(null, GetMd5.getMD5String(originalFilename),originalFilename,contentType,size,url,birthday,birthday,false);
//        marketStorageMapper.insert(marketStorage);

        locations = locations.replace("file:", "");
        file.transferTo(new File(locations, originalFilename));
        String url = "http://localhost:8083/image/" + originalFilename;
        MarketStorage marketStorage = new MarketStorage(null, originalFilename, originalFilename, contentType, size, url, birthday, birthday, false);
       marketStorageMapper.insert(marketStorage);

        return marketStorage;
    }

    @Override
    public void delete(MarketStorage file) {
        marketStorageMapper.deleteByPrimaryKey(file.getId());
    }

    @Override
    public void update(MarketStorage file) {
        file.setUpdateTime(birthday);
        marketStorageMapper.updateByPrimaryKey(file);
    }

    @Override
    public StorageVo query(BaseParam param) {
        StorageVo storageVo = new StorageVo();
        Integer limit = param.getLimit();
        storageVo.setLimit(limit);
        Integer page = param.getPage();
        storageVo.setPage(page);
        String sort = param.getSort();
        String order = param.getOrder();
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketStorageExample example = new MarketStorageExample();
        example.setOrderByClause(sort + " " + order);
        MarketStorageExample.Criteria criteria = example.createCriteria();
        if (param.getKey() != null && param.getName() != null) {
//            MarketStorageExample.Criteria criteria = example.createCriteria();
            criteria.andKeyLike("%" + param.getKey() + "%" + param.getName() + "%");
        }
        if (param.getKey() != null && param.getName() == null) {
//            MarketStorageExample.Criteria criteria = example.createCriteria();
            criteria.andKeyLike("%" + param.getKey() + "%");
        }
        if (param.getName() != null && param.getKey() == null) {
//            MarketStorageExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%" + param.getName() + "%");
        }

        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);
        System.out.println(marketStorages);
        storageVo.setTotal(marketStorages.size());
        storageVo.setList(marketStorages);
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorages);
        int total = (int) pageInfo.getTotal();
        storageVo.setTotal(total);
        int pages = pageInfo.getPages();
        storageVo.setPages(pages);
        return storageVo.data(total, pages, limit, page, marketStorages);
    }


}
